package logic

import (
	"context"
	"errors"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectDelLogic {
	return &CollectDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectDelLogic) CollectDel(in *videorpc.CollectDelTRequest) (*videorpc.CollectDelResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.UserInfo
	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("未登录")
	}

	var collect mysql.CollectInfo
	err = collect.FindCollect(mysql.DB, in.UserId, in.VideoId)
	if err != nil {
		return nil, err
	}
	if collect.ID == 0 {
		return nil, errors.New("未已收藏该视频")
	}
	var vedio mysql.VideoInfo
	vedio.UpdateVedioNum(mysql.DB, in.VideoId)
	collect.CollectDel(mysql.DB)
	return &videorpc.CollectDelResponse{}, nil
}
